/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */
package info.peigen.hotpot.common.core.enums;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.TypeUtil;
import com.alibaba.fastjson2.annotation.JSONCreator;
import com.alibaba.fastjson2.annotation.JSONType;
import com.google.common.collect.Lists;
import info.peigen.hotpot.common.core.annotation.Comment;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <b>(CommentType)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2023/12/12
 */
@Getter
@NoArgsConstructor
@AllArgsConstructor
@JSONType(writeEnumAsJavaBean = true)
public enum CommentType implements Messageable {
    /** 为了对抗 Alibaba Java Coding 而存在的注释 */
    String("String", "字符串"),
    Boolean("Boolean", "布尔"),
    Short("Short", "短整型"),
    Integer("Integer", "整型"),
    Long("Long", "长整型"),
    Double("Double", "双精度浮点"),
    Float("Float", "单精度浮点"),
    Enum("Messageable", "枚举"),
    DateTime("DateTime", "日期"),
    Class("Class", "对象"),
    List("java.util.List", "集合"),
    Json("Json", "JSON"),

    /** 不晓得啷格回事 */
    UNKNOWN("--", "不晓得啷格回事");

    /** 枚举值 */
    String code;

    /** 枚举描述 */
    String message;

    /**
     * 通过枚举<code>code</code>获得枚举
     *
     * @param code 枚举值
     *
     * @return CommentType
     */
    public static CommentType getByCode(String code) {
        return Stream.of(values())
                .filter(innerEnum -> StrUtil.equals(code, innerEnum.code()))
                .findFirst().orElse(UNKNOWN);
    }

    public static CommentType getByField(Field field) {
        Comment comment = field.getAnnotation(Comment.class);
        if (ObjUtil.isNotEmpty(comment) && comment.type() == Json) {
            return Json;
        }

        Type type = TypeUtil.getActualType(field.getType(), field);
        if (TypeUtil.getClass(type).isEnum()) {
            return Enum;
        }

        if (StrUtil.contains(TypeUtil.getClass(type).getTypeName(), List.code())) {
            return List;
        }

        if (StrUtil.equalsIgnoreCase(type.getClass().getTypeName(), Class.code())) {
            return Class;
        }

        for (CommentType innerEnum : values()) {
            if (StrUtil.equalsIgnoreCase(type.getTypeName(), innerEnum.code())
                    || StrUtil.containsIgnoreCase(type.getTypeName(), innerEnum.code())
                    || StrUtil.contains(type.getClass().getName(), innerEnum.code())) {
                return innerEnum;
            }

            if (innerEnum == Class) {
                System.out.println("1");
            }
        }
        return UNKNOWN;
    }

    /**
     * 通过枚举<code>name</code>获得枚举
     *
     * @param name 枚举名
     *
     * @return CommentType
     */
    public static CommentType getByName(String name) {
        return Stream.of(values())
                .filter(innerEnum -> StrUtil.equalsIgnoreCase(name, innerEnum.name()))
                .findFirst().orElse(UNKNOWN);
    }

    @JSONCreator
    public static CommentType deserialize(String code) {
        return Stream.of(values())
                .filter(innerEnum -> StrUtil.equals(code, innerEnum.code()))
                .findFirst().orElse(UNKNOWN);
    }

    /**
     * 获取全部枚举
     *
     * @return List<CommentType>
     */
    public static List<CommentType> getAllEnum() {
        return Lists.newArrayList(values());
    }

    /**
     * 获取全部枚举值
     *
     * @return List<String>
     */
    public static List<String> getAllEnumCode() {
        return getAllEnum().stream().map(CommentType::code).collect(Collectors.toList());
    }

    /**
     * 通过code获取msg
     *
     * @param code 枚举值
     *
     * @return String
     */
    public static String getMsgByCode(String code) {
        return getByCode(code).message();
    }

    /**
     * 获取枚举code
     *
     * @param innerEnum 枚举
     *
     * @return code
     */
    public static String getCode(CommentType innerEnum) {
        return Optional.ofNullable(innerEnum).map(CommentType::code).orElse(UNKNOWN.code());
    }

    /**
     * map
     *
     * @return Map
     */
    public static Map<String, String> maps() {
        return Stream.of(values()).collect(Collectors.toMap(CommentType::code, CommentType::message));
    }

    /**
     * 该方法是为了避免NPE，安全获取枚举中的字段值
     */
    public static <T> T safeGet(CommentType enumValue, Function<CommentType, T> mapper, T defaultValue) {
        return Objects.isNull(enumValue) ? defaultValue : mapper.apply(enumValue);
    }

    /**
     * 该方法是为了避免NPE，安全获取枚举中的字段值，默认返回值是null
     */
    public static <T> T safeGet(CommentType enumValue, Function<CommentType, T> mapper) {
        return safeGet(enumValue, mapper, null);
    }

    /**
     * 该方法是为了避免NPE，安全获取枚举中的code值，默认返回值是null
     */
    public static String safeGetCode(CommentType enumValue) {
        return safeGet(enumValue, Messageable::code, null);
    }

    /**
     * 判断给定的枚举，是否在列表中
     *
     * @param values 列表
     *
     * @return Boolean
     */
    public boolean isInList(CommentType... values) {
        for (CommentType e : values) {
            if (this == e) {
                return true;
            }
        }
        return false;
    }

    /**
     * @return Returns the code.
     */
    @Override
    public String code() {
        return code;
    }

    /**
     * @return Returns the message.
     */
    @Override
    public String message() {
        return message;
    }

    @Override
    public String toString() {
        return message;
    }
}